package com.jty.lib_base.event

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @Description 事件分发中心管理
 * 可通过Kotlin的命名可选参数特性 一个方法即可判断 发送/接收
 * sticky : post/observeSticky
 */
object LiveBusCenter {

//    fun handleMainEvent(
//        value: String? = null,
//        owner: LifecycleOwner? = null,
//        observer:Observer<MainEvent>?=null
//    ) {
//        if (null == owner && value != null) {
//            LiveEventBus.get(MainEvent::class.java).post(MainEvent(value))
//            return
//        }
//        owner?.let{
//            observer?.apply {
//                LiveEventBus.get(MainEvent::class.java).observe(it, this)
//            }
//        }
//    }

    fun postMainEvent(value: String?) {
        LiveEventBus.get(MainEvent::class.java).post(MainEvent(value))
    }

    fun observeMainEvent(owner: LifecycleOwner, func: (t: MainEvent) -> Unit) {
        LiveEventBus.get(MainEvent::class.java).observe(owner, Observer(func))
    }

//    fun postTokenExpiredEvent(value: String?) {
//        LiveEventBus.get(TokenExpiredEvent::class.java).post(TokenExpiredEvent(value))
//    }
//
//    fun observeTokenExpiredEvent(owner: LifecycleOwner, observer: Observer<TokenExpiredEvent>) {
//        LiveEventBus.get(TokenExpiredEvent::class.java).observe(owner, observer)
//    }

    fun postRegisterSuccessEvent(account: String?, pwd: String?) {
        LiveEventBus.get(RegisterSuccessEvent::class.java).post(RegisterSuccessEvent(account, pwd))
    }

    fun observeRegisterSuccessEvent(
        owner: LifecycleOwner,
        func: (t: RegisterSuccessEvent) -> Unit
    ) {
        LiveEventBus.get(RegisterSuccessEvent::class.java).observe(owner, Observer(func))
    }


    fun postLogoutEvent(code: Int) {
        LiveEventBus.get(LogoutEvent::class.java).post(LogoutEvent(code))
    }

    fun observeLogoutEvent(owner: LifecycleOwner, func: (t: LogoutEvent) -> Unit) {
        LiveEventBus.get(LogoutEvent::class.java).observe(owner, Observer(func))
    }

    fun postToVersionUpdateEvent(code: Boolean) {
        LiveEventBus.get(ToVersionUpdateEvent::class.java).post(ToVersionUpdateEvent(code))
    }

    fun observeToVersionUpdateEvent(owner: LifecycleOwner, func: (t: ToVersionUpdateEvent) -> Unit) {
        LiveEventBus.get(ToVersionUpdateEvent::class.java).observe(owner, Observer(func))
    }

    fun postLoginSuccessEvent() {
        LiveEventBus.get(LoginSuccessEvent::class.java).post(LoginSuccessEvent(0))
    }

    fun observeLoginSuccessEvent(owner: LifecycleOwner, func: (t: LoginSuccessEvent) -> Unit) {
        LiveEventBus.get(LoginSuccessEvent::class.java).observe(owner, Observer(func))
    }

    fun postToPenNoSubmitEvent() {
        LiveEventBus.get(ToPenNoSubmitEvent::class.java).post(ToPenNoSubmitEvent(0))
    }

    fun observeToPenNoSubmitEvent(owner: LifecycleOwner, func: (t: ToPenNoSubmitEvent) -> Unit) {
        LiveEventBus.get(ToPenNoSubmitEvent::class.java).observe(owner, Observer(func))
    }


    fun postRefreshUserFmEvent() {
        LiveEventBus.get(RefreshUserFmEvent::class.java).post(RefreshUserFmEvent(0))
    }

    fun observeRefreshUserFmEvent(owner: LifecycleOwner, func: (t: RefreshUserFmEvent) -> Unit) {
        LiveEventBus.get(RefreshUserFmEvent::class.java).observe(owner, Observer(func))
    }

    fun postRefreshWebListEvent() {
        LiveEventBus.get(RefreshWebListEvent::class.java).post(RefreshWebListEvent(0))
    }

    fun observeRefreshWebListEvent(owner: LifecycleOwner, func: (t: RefreshWebListEvent) -> Unit) {
        LiveEventBus.get(RefreshWebListEvent::class.java).observe(owner, Observer(func))
    }

    fun postPenIsConnectEvent(msg:String) {
        LiveEventBus.get(PenIsConnectEvent::class.java).post(PenIsConnectEvent(msg))
    }

    fun observePenIsConnectEvent(owner: LifecycleOwner, func: (t: PenIsConnectEvent) -> Unit) {
        LiveEventBus.get(PenIsConnectEvent::class.java).observe(owner, Observer(func))
    }

    fun postResearchTabUpdateEvent(count:Int){
        LiveEventBus.get(RefreshResearchTabEvent::class.java).post(RefreshResearchTabEvent(count))
    }

    fun observeResearchTabUpdateEvent(owner: LifecycleOwner,func: (t: RefreshResearchTabEvent) -> Unit){
        LiveEventBus.get(RefreshResearchTabEvent::class.java).observe(owner, Observer(func))
    }


    fun posModifyPhoneSuccessEvent(msg:String) {
        LiveEventBus.get(ModifyPhoneSuccessEvent::class.java).post(ModifyPhoneSuccessEvent(msg))
    }

    fun observeModifyPhoneSuccessEventEvent(owner: LifecycleOwner, func: (t: ModifyPhoneSuccessEvent) -> Unit) {
        LiveEventBus.get(ModifyPhoneSuccessEvent::class.java).observe(owner, Observer(func))
    }


}